﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class Customer_Property_Link : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.CustomerPropertyLink> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.CustomerPropertyLink> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string customerID = Request.QueryString["CustomerID"];
            string customerCode = Request.QueryString["CustomerCode"];
            string customerName = Request.QueryString["CustomerName"];

            this.lblCustomerCode.Text = customerCode;
            this.lblCustomerName.Text = customerName;
            this.lblCustomerID.Value = customerID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetCustomerPropertyLinks()
        {
            List<Model.CustomerPropertyLink> lstCustomerProperty = new List<Model.CustomerPropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomerPropertyLink(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.CustomerPropertyLink customerProperty = new Model.CustomerPropertyLink();
                    customerProperty.PropertyLinkID = r["PropertyLinkID"].ToString();
                    customerProperty.CustomerID = r["CustomerID"].ToString();
                    customerProperty.CustomerCode = r["CustomerCode"].ToString();
                    customerProperty.CustomerName = r["CustomerName"].ToString();
                    customerProperty.PropertyDetailID = r["PropertyDetailID"].ToString();
                    customerProperty.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    customerProperty.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstCustomerProperty.Add(customerProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstCustomerProperty };
                return new Response(lstCustomerProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateCustomerPropertyLink(string oCustomerPropertyLink, bool isInsert)
        {
            Model.CustomerPropertyLink customerPropertyLink = JsonConvert.DeserializeObject<Model.CustomerPropertyLink>(oCustomerPropertyLink);

            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonCustomerPropertyLink( customerPropertyLink.CustomerID, customerPropertyLink.PropertyDetailID, customerPropertyLink.PropertyLinkID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}
